/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.dao.bmp.provider;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 动态XbdunPowerRoleDepartDo的SQL语句提供者
 * 部门关联角色权限
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
public class PowerRoleDepartSqlProvider {

	/**
	 * 判断是否存在,不为null则是存在
	 * @param map
	 */
	public String existSql(Map<String,Object> params){
		StringBuilder sbSql = new StringBuilder();
		@SuppressWarnings("unchecked")
		Map<String,Object> mparam = (Map<String,Object>)params.get("param");
		boolean bl = false;
		sbSql.append("SELECT COUNT(*) FROM xbdun_power_role_depart WHERE ");
		if(!params.isEmpty()){
			Iterator<Entry<String, Object>> iparam = mparam.entrySet().iterator();
			while(iparam.hasNext()){
				
				Entry<String, Object> enty = iparam.next();
				if(bl){sbSql.append(" AND ");}
				
				sbSql.append(enty.getKey()+" = "+"#{param."+enty.getKey()+"}");
				
				if(!bl)bl = true;
			}
		}
		
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
	//新增部门角色关联菜单
	public String addPowerRolesDepartSql(Map<String,Object> map){
		@SuppressWarnings("unchecked")
		List<Long> menus = (List<Long>)map.get("menus");		//获取设置的菜单
		Long orgCode = (Long)map.get("orgCode");				//机构ID		
		Long orleDepartId = (Long)map.get("orleDepartId");		//部门角色ID
		
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("INSERT INTO xbdun_power_role_depart(code,orgCode,orleDepartId)");
		sbSql.append(" VALUES");
		boolean bl = false;
		for(Long menuCode : menus){
			if(bl){sbSql.append(",");}
			sbSql.append("("+menuCode+","+orgCode+","+orleDepartId+")");
			if(!bl){bl = !bl;}
		}
		menus.clear();
		map.clear();
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
	
	//删除菜单
	public String delPowerRolesDepartSql(Map<String,Object> map){
		@SuppressWarnings("unchecked")
		List<Long> menus = (List<Long>)map.get("menus");		//获取设置的菜单
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("DELETE FROM xbdun_power_role_depart WHERE orgCode=#{orgCode} AND orleDepartId=#{orleDepartId} AND code IN(");
		boolean bl = false;
		for(Long menuCode : menus){
			if(bl){sbSql.append(",");}
			sbSql.append(menuCode);
			if(!bl){bl = !bl;}
		}
		sbSql.append(")");
		menus.clear();
		String content = sbSql.toString();
		sbSql.setLength(0);
		sbSql = null;
		
		return content;
	}
}
